from utils.tools.FileUtil import read_yaml_file
#from app import db,Base

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base


db = SQLAlchemy()
Base = declarative_base()

def db_init(app):
    UPLOAD_FOLDER = '/path/to/the/uploads'
    ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
    path = "{}/app.yaml".format(app.root_path)
    app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
    app_yaml = read_yaml_file(path)
    db_user = app_yaml['website']['database']['username']
    db_pass = app_yaml['website']['database']['password']
    db_name = app_yaml['website']['database']['dbname']
    db_url = app_yaml['website']['database']['url']
    db_port = app_yaml['website']['database']['port']
    db_uri = "mysql+pymysql://{}:{}@{}:{}/{}".format(db_user, db_pass, db_url, db_port, db_name)
    # print(db_uri)
    app.config["SQLALCHEMY_DATABASE_URI"] = db_uri
    app.config["SQLALCHEMY_POOL_RECYCLE "] = 10
    app.config["SQLALCHEMY_POOL_SIZE  "] = 30
    app.config["SQLALCHEMY_POOL_TIMEOUT  "] = 3
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
#   pass
